package com.webcams.ui.resorts;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.webcams.core.resorts.DownloadListener;
import com.webcams.core.resorts.Resort;
import com.webcams.core.resorts.ResortsManager;
import com.webcams.core.utils.AsyncDownloadBitmap;
import com.webcams.ui.R;


public final class WebcamsUI extends Activity implements DownloadListener {

	static Resort resort;
	protected static List<Bitmap> imgs;
	private GridView gridview;

	protected ProgressDialog progressDialog;
	private int nbImagesNonDownloaded;


	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webcams);     

		Bundle bun = getIntent().getExtras();
		int param1 = bun.getInt("id");
		resort = ResortsManager.getInstance(this).get(param1);

		imgs = new ArrayList<Bitmap>();
		imgs.clear();

		progressDialog = new ProgressDialog(WebcamsUI.this);
		progressDialog.setCancelable(true);
		progressDialog.setMessage(getResources().getString(R.string.webcams_loading));
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.setProgress(0);
		progressDialog.setMax(resort.getWebcamsSize());
		progressDialog.show();


		nbImagesNonDownloaded = resort.getWebcamsSize();
		if(resort.getWebcamsSize() != 0)
			new AsyncDownloadBitmap(this).execute(resort.getWebcam(0).getThumbUrl());


		gridview = (GridView) findViewById(R.id.gridview);        
		gridview.setNumColumns(1);
		gridview.setAdapter(new WebcamsAdapter(WebcamsUI.this));        
		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				launchFullScreenWebcam(resort,position);
			}
		});
	}

	public void imageFinishedLoad(Bitmap b){
		
		nbImagesNonDownloaded--;
		int i = resort.getWebcamsSize()-nbImagesNonDownloaded;
		imgs.add(b);
		progressDialog.incrementProgressBy(i);
		gridview.setAdapter(new WebcamsAdapter(this));        
		gridview.invalidate();
		
		if(nbImagesNonDownloaded == 0)
			progressDialog.dismiss();
		else
			new AsyncDownloadBitmap(this).execute(resort.getWebcam(i).getThumbUrl());
		
	}


	protected void launchFullScreenWebcam(Resort st2, final int j) {
		final Intent i = new Intent(this, FullScreenImageUI.class);
		i.putExtra("image", resort.getWebcam(j).getUrl());
		i.putExtra("type", "dist");
		startActivity(i);

	}


	public class WebcamsAdapter extends BaseAdapter{

		private Context mContext;

		public WebcamsAdapter(Context c) {
			mContext = c;
		}

		@Override
		public int getCount() {
			return imgs.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView imageView;
			DisplayMetrics metrics = new DisplayMetrics(); 
			getWindowManager().getDefaultDisplay().getMetrics(metrics);


			int min = Math.min(metrics.widthPixels,metrics.heightPixels);
			Bitmap tmp = imgs.get(position);

			int maxHeight=tmp.getHeight(), maxWidth=tmp.getWidth();

			if(tmp.getWidth() > min) {
				maxWidth = min;
				maxHeight = tmp.getHeight()*min/tmp.getWidth();
			}

			if(maxHeight > min) {
				maxHeight = min;
				maxWidth = tmp.getWidth()*min/maxHeight;
			}

			if (convertView == null) {  
				imageView = new ImageView(mContext);
				imageView.setLayoutParams(new GridView.LayoutParams(maxWidth, maxHeight));
			} else {
				imageView = (ImageView) convertView;
			}

			imageView.setImageBitmap(tmp);

			return imageView;
		}


	}
}